package com.server.common.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.DruidPasswordCallback;
import com.server.common.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

/**
 * @author zgr
 * @date 2023/7/12 13:58
 * @description
 */
@Slf4j
public class DruidCustomPasswordCallback extends DruidPasswordCallback {

    public static String ENCRYPT_KEY = "Y5MUIOM7BUWI7BQR";

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
//        System.out.println("aes="+AESUtil.AESEncode(ENCRYPT_KEY, "123456"));
        // 获取配置文件中的已经加密的密码（spring.datasource.druid.connect-properties.password）
        String pwd = (String) properties.get("password");
        if (StrUtil.isNotEmpty(pwd)) {
            try {
                // 这里的代码是将密码进行解密，并设置
                String password = AESUtil.decrypt( ENCRYPT_KEY,pwd);
                setPassword(password.toCharArray());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
